package cn.lcsjsxy.yxz.project.project2;

public class CustomerList {

    private Customer[] customers; //保存客户对象的数组
    private int total = 0;  //记录已保存客户对象的数量

    //
    public CustomerList(int totalCustomer) {
        customers = new Customer[totalCustomer];
    }

    //添加客户
    public boolean addCustomer(Customer customer) {
        if (customer != null && total < customers.length) {
            customers[total++] = customer;
            return true;
            //或
            //customers[total++] = customer;
            //return true;
        }
        return false;
    }

    //修改指定索引位置的客户信息
    public boolean replaceCustomer(int index, Customer cust) {
        if (index >= 0 && index < total) {
            customers[index] = cust;
            return true;
        }
        return false;
    }

    //删除指定索引位置上的客户
    /*public boolean deleteCustomer(int index) {
        if (index < 0 || index >= total) {
            return false;
        }
        for (int i = index; i < total - 1; i++) {
            customers[i] = customers[i + 1];
        }
        //
        customers[total - 1] = null;
        total--;
        //或
        //customers[--total] = null;
        return true;
    }*/

    public boolean deleteCustomer(int index) {
        if (index >= 0 && index < total) {
            for (int i = index; i < total - 1; i++) {
                customers[i] = customers[i + 1];
            }
//			customers[total - 1] = null;
//			total--;//存储的总人数减少1.
            customers[--total] = null;
            return true;
        }
        return false;
    }

    //获取所有的客户信息
    public Customer[] getAllCustomers() {
        Customer[] custs = new Customer[total];
        for (int i = 0; i < custs.length; i++) {
            custs[i] = customers[i];
        }
        return custs;
    }

    //获取指定索引位置上的客户
    public Customer getCustomer(int index) {
        if (index < 0 || index >= total) {
            //return customers[index];
            return null;
        }
        //return null;
        return customers[index];
    }

    //获取客户的数量
    public int getTotal() {
        return total;
    }

}
